<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------

namespace app\index\controller;
use app\common\controller\Home;

class Oneself extends Home {
    //login
    public function _initialize() {
        if (!is_login()){
            $this->redirect('index/index/login');
        }
    }

    public function index(){
        $this->assign('number',model('Xinjian')->sessionId());
        return $this->fetch();
    }
    public function selfDetail($id){
        $list  = model('Member')->where(array('uid'=>$id))->find();
        $list['birthday'] = date('Y')-intval(substr($list['birthday'], 0, 4));
        $this->assign('list',$list);
        return $this->fetch();
    }
    /*编辑个人资料*/
    public function edit(){
        $this->redirect('index/index/register02', array('panduan' => $_SESSION['index']['user_auth']['uid']));
    }
    /*从session写给$id*/
    public function xinjian(){
        if ($_POST){
            if($_POST['shouxin_id'] != $_SESSION['index']['user_auth']['uid']){
                $_POST['jixin_id'] = $_SESSION['index']['user_auth']['uid'];
                $_POST['shijian'] = date('Y-m-d H:i:s', time());
                $uid = db('xinjian')->insert($_POST);
                if($uid>0){
                    return $this->success('发送成功', url('index/oneself/selfDetail', array('id' => $_POST['shouxin_id'])));
                }else{
                    return $this->error('系统错误，请稍后重试', 'index/oneself/index');
                }
            }else{
                return $this->error('不能给自己写信', '');
            }
        }else{
            $this->redirect('index/oneself/index');
        }
    }
    /*写信*/
    public function xiexin($id){
        $list  = model('Member')->where(array('uid'=>$id))->find();
        $this->assign('username',$list['username']);
        $this->assign('uid',$id);
        return $this->fetch();
    }
    /*信件列表*/
    public function xinlist($id){
        $list  = model('Xinjian')->liebiao($id);
        if(1 == $id){
            $this->setMeta('发件箱');
        }else{
            $this->setMeta('收件箱');
        }
        $this->assign('model_list',$list);
        return $this->fetch();
    }
    /*查看信件--在写一封*/
    public function lookxin($id){
        $list  = model('Xinjian')->xindetail($id);
        if($list){
            $this->assign('model_list',$list);
            return $this->fetch();
        }else{
            return $this->error('信件不存在或是已被删除');
        }
    }
    /*删除信件*/
    public function shanchu(){
        $result = model('Xinjian')->quchu($_POST['thisId']);
        if ($result > 0){
            return $this->success('删除成功...'/*,url('index/oneself/index')*/);
        } else {
            return $this->error('系统错误,请联系管理员...');
        }
    }
    /*通知*/
    public function notice(){
        $lists = model('Notice')->notice();
        $this->setMeta('通知列表');
        $this->assign('lists',$lists);
        return $this->fetch();
    }
    /*积分*/
    public function score(){
        $result = model("member")->where(array('uid' => $_SESSION['index']['user_auth']['uid']))->find();
        $this->assign('score',$result['score']);
        return $this->fetch();
    }
    //礼物
    public function lwList($id){
        $list  = model('Liwu')->liebiao($id);
        if(1 == $id){
            $this->setMeta('收到的礼物');
        }else{
            $this->setMeta('发送的礼物');
        }
        foreach ($list as $key=>$val){
            $list[$key] = $val;
        }
        $this->assign('lists',$list);
        return $this->fetch();
    }
    /*删除礼物*/
    public function scliwu(){
        $result = model('Liwu')->quchu($_POST['thisId']);
        if ($result > 0){
            return $this->success('删除成功...');
        } else {
            return $this->error('系统错误,请联系管理员...');
        }
    }
}
